﻿#include "EarthGolem.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../SpellObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char EarthGolem::getAttackRange() const
    {
        return 8;
    }

    EarthGolem::EarthGolem(MonsterInfo *info) : ZumaMonster(info)
    {
    }

    bool EarthGolem::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void EarthGolem::ProcessAI()
    {
        if (!Dead && getEnvir()->getTime() > ActionTime)
        {
            bool stoned = !FindNearby(4);

            if (Stoned && !stoned)
            {
                Wake();

                AttackTime = getEnvir()->getTime() + 1500;
                ActionTime = getEnvir()->getTime() + 1500;
                MoveTime = getEnvir()->getTime() + 1500;
            }
        }

        ZumaMonster::ProcessAI();
    }

    void EarthGolem::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (!ranged && getEnvir()->Random->Next(3) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MAC});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar2);

            auto location = getTarget()->getCurrentLocation();

            for (int y = location->Y - 1; y <= location->Y + 1; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = location->X - 1; x <= location->X + 1; x++)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }

                    if (x == getCurrentLocation()->X && y == getCurrentLocation()->Y)
                    {
                        continue;
                    }

                    auto cell = getCurrentMap()->GetCell(x, y);

                    if (!cell->getValid())
                    {
                        continue;
                    }

                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MinDC]);

                    auto start = 500;

                    SpellObject *ob = new SpellObject();
                    ob->Spell = Spell::EarthGolemPile;
                    ob->Value = damage;
                    ob->ExpireTime = getEnvir()->getTime() + 1200 + start;
                    ob->TickSpeed = 1000;
                    Point tempVar3(x, y);
                    ob->setCurrentLocation(&tempVar3);
                    ob->CastLocation = location;
                    ob->Show = location->X == x && location->Y == y;
                    ob->setCurrentMap(getCurrentMap());

                    DelayedAction *action = new DelayedAction(DelayedType::Spawn, getEnvir()->getTime() + start, {ob});
                    getCurrentMap()->ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete ob' statement was not added since ob was passed to a method or constructor. Handle memory management manually.
                }
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void EarthGolem::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && getEnvir()->getTime() < FearTime)
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 2000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist >= getAttackRange())
        {
            MoveTo(getTarget()->getCurrentLocation());
        }
        else
        {
            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }

        }
    }
}
